
package com.cptravel.activity;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;

import com.core.api.ApiListenrDefaultImpl;
import com.core.api.B5MBaseResponse;
import com.core.controller.B5MBaseActivity;
import com.core.eventbus.EventBus;
import com.core.util.B5MPreferenceHelper;
import com.core.util.B5MPreferenceKeys;
import com.core.util.B5MToaster;
import com.cptravel.R;
import com.cptravel.api.RegisterRequest;
import com.cptravel.api.RegisterResponse;
import com.cptravel.tool.EventNames;
import com.cptravel.tool.VerifyData;

public class RegisterActivity extends B5MBaseActivity implements OnClickListener
{
    private EditText et_username, et_gender, et_pwd, et_againpwd, et_tel, et_email;
    private Dialog mDialog;
    private RegisterRequest mRegisterRequest;
    private String userName;
    private String pwd;

    @Override
    protected String getHeadTitle()
    {
        // TODO Auto-generated method stub
        return "注册";
    }

    @Override
    public int doGetContentViewId()
    {
        // TODO Auto-generated method stub
        return R.layout.register;
    }

    @Override
    public void doInitSubViews(View containerView)
    {
        // TODO Auto-generated method stub
        et_username = (EditText) findViewById(R.id.et_username);
        et_gender = (EditText) findViewById(R.id.et_gender);
        et_pwd = (EditText) findViewById(R.id.et_pwd);
        et_againpwd = (EditText) findViewById(R.id.et_againpwd);
        et_tel = (EditText) findViewById(R.id.et_tel);
        et_email = (EditText) findViewById(R.id.et_email);
        et_gender.setOnClickListener(this);
    }

    @Override
    public void doInitDataes()
    {
        // TODO Auto-generated method stub
        final String[] genders = { "男", "女" };
        mDialog = new AlertDialog.Builder(mContext).setSingleChoiceItems(genders, 0, new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog , int which)
            {
                // TODO Auto-generated method stub
                dialog.dismiss();
                et_gender.setText(genders[which]);
            }
        }).create();
        mRegisterRequest = new RegisterRequest(new ApiListenrDefaultImpl()
        {
            @Override
            public void onSuccess(B5MBaseResponse b5mBaseResponse)
            {
                // TODO Auto-generated method stub
                super.onSuccess(b5mBaseResponse);
                RegisterResponse registerResponse = (RegisterResponse) b5mBaseResponse;
                B5MPreferenceHelper.saveStringValue(mContext, B5MPreferenceKeys.USER_USERID, registerResponse.userid);
                B5MPreferenceHelper.saveStringValue(mContext, B5MPreferenceKeys.USER_NAME, userName);
                B5MPreferenceHelper.saveStringValue(mContext, B5MPreferenceKeys.USER_PWD, pwd);
                EventBus.getInstance().notifiDataUpdate(EventNames.REGISTER_SUCCESS, true);
                finish();
            }
        }, mProgressBar);
    }

    @Override
    public void doAfter()
    {
        // TODO Auto-generated method stub
    }

    public void commit(View view)
    {
        userName = et_username.getText().toString().trim();
        String gender = et_gender.getText().toString().trim();
        pwd = et_pwd.getText().toString().trim();
        String againPwd = et_againpwd.getText().toString().trim();
        String tel = et_tel.getText().toString().trim();
        String email = et_email.getText().toString().trim();
        if (TextUtils.isEmpty(userName))
        {
            toast("用户名不能为空");
            return;
        }
        if (!VerifyData.verifyUserName(userName))
        {
            toast("用户名格式不正确");
            return;
        }
        if (TextUtils.isEmpty(gender))
        {
            toast("性别不能为空");
            return;
        }
        if (TextUtils.isEmpty(pwd))
        {
            toast("密码不能为空");
            return;
        }
        if (!VerifyData.verifyPwd(pwd))
        {
            toast("密码格式不正确");
            return;
        }
        if (TextUtils.isEmpty(againPwd) || !pwd.equals(againPwd))
        {
            toast("两次密码不一致");
            return;
        }
        if (TextUtils.isEmpty(tel))
        {
            toast("手机号不能为空");
            return;
        }
        if (!VerifyData.verifyPhone(tel))
        {
            toast("手机号格式不对");
            return;
        }
        if (TextUtils.isEmpty(email))
        {
            toast("邮箱不能为空");
            return;
        }
        if (!VerifyData.verifyEmail(email))
        {
            toast("邮箱格式不正确");
            return;
        }
        mRegisterRequest.userName = userName;
        mRegisterRequest.gender = gender;
        mRegisterRequest.pwd = pwd;
        mRegisterRequest.againPwd = againPwd;
        mRegisterRequest.tel = tel;
        mRegisterRequest.email = email;
        mRegisterRequest.start();
    }

    public void toast(String string)
    {
        B5MToaster.showShort(mActivity, string, 0);
    }

    @Override
    public void onClick(View v)
    {
        // TODO Auto-generated method stub
        switch (v.getId())
        {
            case R.id.et_gender:
                mDialog.show();
                break;
            default:
                break;
        }
    }
}
